package com.bw.controller;

import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class CookieController {


    /**
     * 有浏览器第一次访问服务器
     * @return
     */
    @RequestMapping("/one.do")
    public String one(HttpServletResponse response){ // 代表 服务器要返回给浏览器的数据
        // 构建一个cookie
        Cookie cookie = new Cookie("cookie-name", "cookie-value");
        // 把cookie返回给浏览器
        response.addCookie(cookie);

        return "success";

    }



    /**
     * 有浏览器第二次访问服务器
     * @return
     */
    @RequestMapping("/two.do")
    public String two(HttpServletRequest request){ // request  代表浏览器发过来的所有数据


        Cookie[] cookies = request.getCookies();

        System.out.println(JSON.toJSONString(cookies));

        return "success";

    }
}
